package com.walker.kuyun.ui.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.walker.kuyun.App
import com.walker.kuyun.R
import com.walker.kuyun.bean.VideoCoverBean
import com.walker.kuyun.config.Const
import com.walker.kuyun.util.Cover7ViewHolder
import com.walker.kuyun.util.Util.process
import com.walker.kuyun.util.Util.showToast
import com.walker.kuyun.util.ViewHolderUtil.Companion.getItemViewType
import com.walker.kuyun.util.ViewHolderUtil.Companion.getViewHolder
import com.walker.kuyun.util.invisible
import com.walker.kuyun.util.visible
import com.walker.kuyun.ui.activity.DownloadActivity

class TMDownloadAdapter(
    val activity: DownloadActivity,
    private val dataList: List<VideoCoverBean>
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun getItemViewType(position: Int): Int = getItemViewType(dataList[position])

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecyclerView.ViewHolder =
        getViewHolder(parent, viewType)

    override fun getItemCount(): Int = dataList.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = dataList[position]

        when (holder) {
            is Cover7ViewHolder -> {
                holder.tvAnimeCover7Title.isFocused = true
                holder.tvAnimeCover7Title.text = item.title
                holder.tvAnimeCover7Size.isFocused = true
                holder.tvAnimeCover7Size.text = item.size
                if (item.actionUrl.startsWith(Const.ActionUrl.ANIME_ANIME_DOWNLOAD_EPISODE)) {
                    holder.tvAnimeCover7Episodes.text = item.episodeCount
                    holder.tvAnimeCover7Episodes.visible()
                } else {
                    holder.tvAnimeCover7Episodes.invisible()
                }
                holder.itemView.setOnClickListener {
                    process(activity, item.actionUrl)
                }
            }
            else -> {
                holder.itemView.visibility = View.GONE
                (App.getApp().resources.getString(R.string.unknown_view_holder) + position).showToast()
            }
        }
    }
}